﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;

namespace MvcWeb.Controllers
{
    public class ConfigOtherController : BaseController
    {
        //
        // GET: /ConfigOther/

        public ActionResult Index(int pageindex = 1, int pageSize = 50, int typeId = -1)//(int)MvcWeb.EAMEnum.ConfigType.资产状态)
        {
            var iquery = db.AssetConfigOther.AsQueryable();

            if (typeId != -1)
            {
                iquery = iquery.Where(m => m.AssetConfigType == typeId);
            }

            iquery = iquery.OrderBy(m => m.AssetConfigID);//.Where(m => m.AssetConfigType == typeId)

            var list = iquery.ToPagedList(pageindex, pageSize);

            var dict = db.AssetConfigOtherType.ToList().ToDictionary(a => a.configOtherTypeId, b => b.ConfigOtherTypeName);
            ViewData["confOTypeDic"] = dict;

            return View(list);
        }

        public ActionResult Edit(int id = -1)
        {
            EAM.Model.AssetConfigOther model = db.AssetConfigOther.Find(id);            

            var dict = MvcWeb.EAMEnum.EAMEnumExt.ToDictory<MvcWeb.EAMEnum.ConfigType>();

            ViewData["AssetConfigTypeList"] = dict;

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(int id, EAM.Model.AssetConfigOther viewModel)
        {
            var user = System.Web.Security.Membership.GetUser();
            var guid = (Guid)user.ProviderUserKey;

            if (id == -1)
            {
                //添加关联信息
                EAM.Model.AssetConfigOther model = new EAM.Model.AssetConfigOther()
                {
                    AssetConfigName = viewModel.AssetConfigName,
                    AssetConfigDescprit = viewModel.AssetConfigDescprit,
                    AssetConfigType = viewModel.AssetConfigType,
                    LastUpdateMember = guid,
                    LastUpdateTime = DateTime.Now
                };
                db.AssetConfigOther.Add(model);
            }
            else
            {
                var model = db.AssetConfigOther.Find(id);
                db.AssetConfigOther.Attach(model);

                model.AssetConfigName = viewModel.AssetConfigName;
                model.AssetConfigDescprit = viewModel.AssetConfigDescprit;
                model.AssetConfigType = viewModel.AssetConfigType;
                model.LastUpdateMember = guid;
                model.LastUpdateTime = DateTime.Now;
            }

            db.SaveChanges();
            return RedirectToAction("index");
        }

        
        public ActionResult del(int id)
        {
            //资产中所有的删除都不允许
            //EAM.Model.AssetConfigOther model = db.AssetConfigOther.Find(id);
            //db.AssetConfigOther.Remove(model);
            return Json(true);
        }

        public ActionResult DeleteChoose()
        {
            //资产中所有的删除都不允许
            //string ids = Request.Form["ids"];
            //if (!string.IsNullOrWhiteSpace(ids))
            //{
            //    string[] arr = ids.Split(',');
            //    foreach (var m in arr)
            //    {
            //        var model = db.AssetConfigOther.Find(int.Parse(m));
            //        db.AssetConfigOther.Remove(model);
            //    }
            //    db.SaveChanges();
            //}
            return Json(true);
        }

    }
}
